package com.richrail;

import java.util.LinkedHashSet;
import java.util.Set;

import com.richrail.state.DepotState;
import com.richrail.state.StateProvider;
import com.richrail.util.Observable;
import com.richrail.util.Observer;

public class Depot implements Observable
{
	// They must be unique and sorted, because it makes no sense to notify the same object twice.
	private Set<Observer> observers = new LinkedHashSet<Observer>();
	
	private StateProvider stateProvider;
	
	public void setStateProvider(StateProvider stateProvider)
	{
		this.stateProvider = stateProvider;
	}
	
	public DepotState getState()
	{
		return stateProvider.getState();
	}
	
	@Override
	public void attachObserver(Observer observer)
	{
		observers.add(observer);
	}

	@Override
	public void detachObserver(Observer observer)
	{
		observers.remove(observer);
	}

	@Override
	public void notifyObservers()
	{
		for (Observer observer: observers)
		{
			observer.update(this);
		}
	}
}
